package com.gf.forcesystem.model.dto;

import com.gf.framework.common.exception.BizException;
import com.gf.framework.common.json.GfJsonMapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * @author gaofei
 * @date 2022/6/19 15:47
 * 无人机状态帧
 */
@Data
public class UAVState implements IMessage{
    @ApiModelProperty("同步字节，无意义")
    private Short syncByte;
    @ApiModelProperty("帧长度")
    private Short frameLength;
    @ApiModelProperty("识别符，无意义")
    private Byte identifier;
    @ApiModelProperty("飞机编号")
    private Byte uavId;
    @ApiModelProperty("飞机类型")
    private Byte uavType;
    /**
     * 0x00：正常
     * 0x10：起落架故障
     * 0x11：发动机故障
     * 0x12：机翼舵故障
     * 0x13：尾翼舵故障
     * 0x20：任务载荷故障
     */
    @ApiModelProperty("飞机当前状态")
    private Byte uavState;
    @ApiModelProperty("飞机经度 10^6")
    private Integer longitude;
    @ApiModelProperty("飞机纬度 10^6")
    private Integer latitude;
    @ApiModelProperty("飞机海拔高度")
    private Short height;
    @ApiModelProperty("飞机航向 0-360 10^2")
    private Short heading;
    @ApiModelProperty("飞机俯仰")
    private Short pitch;
    @ApiModelProperty("飞机翻滚")
    private Short roll;
    @ApiModelProperty("飞机空速")
    private Byte airSpeed;
    @ApiModelProperty("飞机地速")
    private Byte groundSpeed;
    @ApiModelProperty("飞机燃油量")
    private Short fuel;
    @ApiModelProperty("飞机燃油量百分比0-100")
    private Byte fuelPercentage;
    @ApiModelProperty("飞机油门")
    private Byte throttle;
    @ApiModelProperty("飞机螺旋桨转速")
    private Short propellerSpeed;
    @ApiModelProperty("待定，无意义")
    private byte[] undetermined1 = new byte[9];

    @ApiModelProperty("任务载荷类型")
    private Byte taskLoadType;
    @ApiModelProperty("任务载荷厂家编号")
    private Byte taskLoadManufactorId;
    @ApiModelProperty("载荷系统状态")
    private Byte loadSystemStatus;
    @ApiModelProperty("载荷方位角")
    private Short loadAzimuth;
    @ApiModelProperty("载荷俯仰角")
    private Short loadPitch;
    @ApiModelProperty("载荷当前活动传感器")
    private Byte loadCurrentSensor;
    @ApiModelProperty("载荷当前活动传感器焦距")
    private Short loadCurrentSensorFocalLength;
    @ApiModelProperty("载荷当前活动传感器水平视场")
    private Short loadCurrentSensorHorizontal;
    @ApiModelProperty("载荷当前活动传感器垂直视场")
    private Short loadCurrentSensorVertical;
    @ApiModelProperty("载荷激光照射状态")
    private Byte loadLaserIrradiationStatus;
    @ApiModelProperty("载荷激光测距值")
    private Short loadLaserRangingValue;
    @ApiModelProperty("载荷搜索区域是否有效")
    private Byte loadSearchAreaValid;

    @ApiModelProperty("左下角经度")
    private Integer leftDownLongitude;
    @ApiModelProperty("左下角纬度")
    private Integer leftDownLatitude;
    @ApiModelProperty("左上角经度")
    private Integer leftTopLongitude;
    @ApiModelProperty("左上角纬度")
    private Integer leftTopLatitude;
    @ApiModelProperty("右上角经度")
    private Integer rightTopLongitude;
    @ApiModelProperty("右上角纬度")
    private Integer rightTopLatitude;
    @ApiModelProperty("右下角经度")
    private Integer rightDownLongitude;
    @ApiModelProperty("右下角纬度")
    private Integer rightDownLatitude;
    @ApiModelProperty("待定，无意义")
    private byte[] undetermined2 = new byte[10];

    @ApiModelProperty("挂架数量")
    private Byte numberOfPylons;
    @ApiModelProperty("挂架0类型")
    private Byte pylon0Type;
    @ApiModelProperty("悬挂物0-3状态")
    private byte[] hangingObject0State = new byte[4];
    @ApiModelProperty("挂架1类型")
    private Byte pylon1Type;
    @ApiModelProperty("悬挂物0-3状态")
    private byte[] hangingObject1State = new byte[4];
    @ApiModelProperty("挂架2类型")
    private Byte pylon2Type;
    @ApiModelProperty("悬挂物0-3状态")
    private byte[] hangingObject2State = new byte[4];
    @ApiModelProperty("挂架3类型")
    private Byte pylon3Type;
    @ApiModelProperty("悬挂物0-3状态")
    private byte[] hangingObject3State = new byte[4];
    @ApiModelProperty("挂架4类型")
    private Byte pylon4Type;
    @ApiModelProperty("悬挂物0-3状态")
    private byte[] hangingObject4State = new byte[4];
    @ApiModelProperty("挂架5类型")
    private Byte pylon5Type;
    @ApiModelProperty("悬挂物0-3状态")
    private byte[] hangingObject5State = new byte[4];
    @ApiModelProperty("待定，无意义")
    private byte[] undetermined3 = new byte[11];
    @ApiModelProperty("校验和")
    private Byte checksum;

    public static UAVState loadFromBytes(byte[] bytes) throws BizException, IllegalAccessException {
        if (bytes.length != 144) {
            throw new BizException("解析UAVState数据失败，字节长度不对");
        }
        UAVState ret = new UAVState();
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        Field[] fields = UAVState.class.getDeclaredFields();
        for (Field field : fields) {
            Object obj = null;
            if (field.getType().equals(Byte.class)) {
                obj = byteBuffer.get();
            }
            else if (field.getType().equals(Short.class)) {
                obj = byteBuffer.getShort();
            }
            else if (field.getType().equals(Integer.class)) {
                obj = byteBuffer.getInt();
            }
            else if (field.getType().equals(byte[].class)) {
                byteBuffer.get((byte[])field.get(ret));
                continue;
            }
            field.set(ret, obj);
        }
        return ret;
    }

    public static void main(String[] args) throws IllegalAccessException {
        UAVState uavState = UAVState.loadFromBytes(new byte[144]);
        System.out.println(GfJsonMapper.obj2String(uavState));
    }

}
